<?php
namespace app\user\controller;
use app\common\controller\UserBase;
use think\facade\View;
use think\facade\Request;
use think\facade\Session;
use app\common\model\User as UserModel;
use app\user\model\UserAuthRule;
use app\user\model\User;
use app\user\model\Notice;

class Index extends UserBase
{
    protected $noLogin = ['login', 'qrcode_login', 'captcha'];
    protected $noAuth = ['index','home','uploadImage','uploadFile', 'uploadVideo', 'icon', 'logout'];
    
    //后台首页
    public function index()
    {
        //获取导航
        $navbar = list_to_tree(UserAuthRule::getNavbar());
        $user = User::with(['shop'])->where(['id'=> UserId()])->find();
        $notice = Notice::whereDay('datetime')->where('status','0')->count();
        return View::fetch('index',['navbar'=>$navbar,'user'=>$user,'notice'=>$notice]);
    }
    //首页
    public function home()
    {
        $action = $this->request->action();
        return View::fetch('home',['action'=>$action]);
    }
    //登陆
    public function login()
    {
        is_user_login() && $this->redirect(url('user/index/index')); // 登录直接跳转
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //是否验证码
            if(sys_set('is_captcha')=='1'){
                //验证规则
                try{
                    $this->validate($param, ['captcha|验证码'=>'require|captcha']);
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            //登陆方式
            $user = new UserModel();
            if(sys_set('login_type')=='email'){
                $user = $user->where(['email' => $param['username'],'password' => md5($param['password'])]);
            }else if(sys_set('login_type')=='username'){
                $user = $user->where(['username' => $param['username'],'password' => md5($param['password'])]);
            }else{
                $user = $user->where(['mobile' => $param['username'],'password' => md5($param['password'])]);
            }
            $admin = $user->with('shop')->order('id desc')->find();
            if ($admin) {
                $admin['status'] != 1 && $this->error('账号已禁用');
                //检查是否过期
                if($admin['exp_time'] < date('Y-m-d',time())){
                    $this->error('您的账户已过期');
                }
                // 保存状态
                $auth = [
                    'user_id' => $admin['id'],
                    'shop_id' => $admin['shop_id'],
                    'exp_time' => $admin['exp_time'],
                    'mobile' => $admin['mobile'],
                ];
                Session::set('user_auth', $auth);
                Session::set('user_auth_sign', data_auth_sign($auth));
                // 更新信息
                UserModel::update([
                    'last_login_time' => time(),
                    'last_login_ip'   => $this->request->ip(),
                    'login_count'     => $admin['login_count'] + 1,
                ],['id' => $admin['id']]);
                insert_user_log('登录了后台系统');
                $this->success('登录成功', url('@user'));
            } else {
                $this->error('账号或密码错误');
            }
        }
        return View::fetch('login');
    }
    //扫码登陆
    public function qrcode_login()
    {
        //$code = "12345";
        $code = new QRcode();
        $code_path = $code->png('111111','',8)->show(); //获取二维码生成的地址
        return "<img src='".$code_path."'>";
    }
    //验证码
    public function captcha($id='')
    {
        return captcha($id);
    }
    // 退出登录
    public function logout()
    {
        insert_user_log('退出了后台系统');
        session('user_auth', null);
        session('user_auth_sign', null);
        return $this->redirect(url('user/index/login'));
    }
}
